﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace ST.Common.Util.Text
{
	public class ChineseHelper
	{
		/// <summary> 
		/// 汉字转化为拼音
		/// 
		/// 测试字符串：你好，abc,简！媜!,飛~機%12单
		/// </summary> 
		/// <param name="str">汉字</param> 
		/// <returns>全拼</returns> 
		public static string GetPYCodeFull(string words)
		{
			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary> 
		/// 汉字转化为拼音首字母
		/// </summary> 
		/// <param name="str">汉字</param> 
		/// <returns>首字母</returns> 
		public static string GetPYCodeInitial(string words)
		{

			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary>
		/// 汉字简体转换成繁体
		/// </summary>
		/// <param name="words"></param>
		/// <returns></returns>
		public static string CHS2CHT(string words)
		{
			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary>
		/// 汉字繁体转换成简体
		/// </summary>
		/// <param name="words"></param>
		/// <returns></returns>
		public static string CHT2CHS(string words)
		{
			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary>
		/// 获取大写人民币
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string ToMoneyUpper(decimal num)
		{
			string s = num.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");//d + "\n" +
			string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
			return Regex.Replace(d, ".", delegate (Match m) { return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
		}

		/// <summary>
		/// 钱币转换成银行规定的大写形式
		/// </summary>
		/// <param name="num"></param>
		/// <returns></returns>
		public static string GetChineseFormatNumber(decimal num)
		{
			return "人民币" + GetChineseFormatNumberWithoutPrefix(num);
		}

		/// <summary>
		/// 钱币转换成银行规定的大写形式（不加前缀“人民币”）
		/// 
		/// 阿拉伯小写金额数字前面，均应填写人民币符号￥
		/// 中文大写金额数字前应标明”人民币”字样，大写金额数字应紧接”人民币”字样填写，不得留有空白
		/// GetChineseFormatNumber(1409.50m);//壹仟肆佰零玖元伍角
		/// GetChineseFormatNumber(6007.14m);//陆仟零柒元壹角肆分
		/// GetChineseFormatNumber(1680.32m);//壹仟陆佰捌拾元零叁角贰分    壹仟陆佰捌拾元叁角贰分
		/// GetChineseFormatNumber(107000.53m);//壹拾万柒仟元零伍角叁分   壹拾万零柒仟元伍角叁分
		/// GetChineseFormatNumber(16409.02m);//壹万陆仟肆佰零玖元零贰分
		/// GetChineseFormatNumber(325.04m);//叁佰贰拾伍元零肆分
		/// GetChineseFormatNumber(345m);
		/// </summary>
		/// <param name="num"></param>
		/// <returns></returns>
		public static string GetChineseFormatNumberWithoutPrefix(decimal num)
		{
			if (num == 0)
			{
				return "零元整";
			}
			string s = num.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
			string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
			string strNum = Regex.Replace(d, ".", delegate (Match m)
			{
				return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString();
			});
			if (strNum.IndexOf('角') < 0 && strNum.IndexOf('分') < 0)
			{
				if (num < 0)
				{
					return "负" + strNum + "整";
				}
				else
				{
					return strNum + "整";
				}
			}
			else
			{
				if (num < 0)
				{
					return "负" + strNum;
				}
				else
				{
					return strNum;
				}
			}
		}

		/// <summary>
		/// 转换成日期大写（字符串分割）
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static string GetChineseFormatDate(string date)
		{
			if (string.IsNullOrEmpty(date))
			{
				return "";
			}
			DateTime dt;
			DateTime.TryParseExact(date.Replace("-", "").Replace("/", ""), "yyyyMMdd", null, DateTimeStyles.None, out dt);
			return GetChineseFormatDate(dt);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="num"></param>
		/// <returns></returns>
		public static string GetChineseNumber(int num)
		{
			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary>
		/// 转换成日期大写
		/// 
		/// 在填写月、日时，月为壹、贰和壹拾的，日为壹至玖和壹拾、贰拾和叁拾的，应在其前加”零”
		/// 日为拾壹至拾玖的，应在其前加”壹”
		/// 1月15日，应写成零壹月壹拾伍日
		/// 10月20日，应写成零壹拾月零贰拾日
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static string GetChineseFormatDate(DateTime dt)
		{
			throw new NotImplementedException();
			//string r = string.Empty;
			//return r;
		}

		/// <summary>
		/// 金额抹零处理
		/// </summary>
		public static string FormatDecimal(decimal d, DecimalFormats format, int decimals)
		{
			if (format == DecimalFormats.Round)
			{
				return Math.Round(d, decimals).ToString("N" + decimals);
			}
			else
			{
				string str = d.ToString("N" + (decimals + 1));
				return str.Substring(0, str.IndexOf('.') + decimals + 1);
			}
		}

		/// <summary>
		/// 返回时间差（例如xx小时前，xx分钟前）
		/// </summary>
		/// <param name="DateTime1"></param>
		/// <param name="DateTime2"></param>
		/// <returns></returns>
		public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
		{
			string dateDiff = null;
			try
			{
				//TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
				//TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
				//TimeSpan ts = ts1.Subtract(ts2).Duration();
				TimeSpan ts = DateTime2 - DateTime1;
				if (ts.Days >= 1)
				{
					dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
				}
				else
				{
					if (ts.Hours > 1)
					{
						dateDiff = ts.Hours.ToString() + "小时前";
					}
					else
					{
						dateDiff = ts.Minutes.ToString() + "分钟前";
					}
				}
			}
			catch
			{
			}
			return dateDiff;
		}
	}

	/// <summary>
	/// 金额处理方式
	/// </summary>
	public enum DecimalFormats
	{
		/// <summary>
		/// 抹零
		/// </summary>
		Truncate,
		/// <summary>
		/// 四舍五入
		/// </summary>
		Round
	}
}
